<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: BlogGateway.cfc 948 2006-08-24 01:24:39Z mpwoodward $

Notes:
--->
<cfcomponent 
	displayname="BlogGateway" 
	output="false" 
	extends="BlogGateway" 
	hint="BlogGateway for MachBlog (MSSQL)">
	
	<cffunction name="init" access="public" output="false" returntype="BlogGateway" 
			hint="Constructor for this CFC">
		<cfreturn this />
	</cffunction>
	
	<cffunction name="getDaysBlogging" access="public" output="false" returntype="numeric" 
			hint="Returns the number of days a particular user has been blogging, or if no id is passed, the total number of days since the first post.">
		<cfargument name="userId" type="string" required="false" default="" />
		
		<cfset var getFirstEntryDate = "" />
		<cfset var daysBlogging = 0 />
		
		<cftry>
			<cfquery name="getFirstEntryDate" datasource="#getDatasource().getDsn()#" 
					username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#" 
					maxrows="1">
				SELECT dt_posted 
				FROM machblog_entry 
				<cfif arguments.userId NEQ "">
				WHERE created_by_id = <cfqueryparam value="#arguments.userId#" cfsqltype="cf_sql_char" maxlength="35" />
				</cfif>
				ORDER BY dt_posted ASC
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
		
		<cfif IsQuery(getFirstEntryDate) AND getFirstEntryDate.RecordCount GT 0>
			<cfset daysBlogging = dateDiff("d", getResourceBundleFacade().getLocaleUtils().fromEpoch(getFirstEntryDate.dt_posted), Now()) />
		</cfif>
		
		<cfreturn daysBlogging />
	</cffunction>
</cfcomponent>
